/**
 *
 */
package com.gp.library.utils;

import android.content.Context;
import android.text.format.DateFormat;

import androidx.annotation.Nullable;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by jackdeng on 2021/4/13.
 */

public class DateTimeUtil {
    public static final int SECOND_LENGTH = 10;
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_HHMMSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_HHMMSS_2 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_HHMM = new SimpleDateFormat("yyyy-MM-dd  HH:mm", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_HHMM2 = new SimpleDateFormat("yyyy.MM.dd  HH:mm", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_3 = new SimpleDateFormat("yyyy.MM.dd", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_T_HHMMSS_Z = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
    public static final SimpleDateFormat DATE_FORMAT_YYYYMMDD_HHMMSS_FOR_FILE_NAME = new SimpleDateFormat("yyyy-MM-dd HH mm ss", Locale.getDefault());

    public static final SimpleDateFormat DATE_FORMAT_WEEK = new SimpleDateFormat("EEEE", Locale.getDefault());

    static {
        DATE_FORMAT_YYYYMMDD_T_HHMMSS_Z.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    /**
     * format the time according to the current locale and the user's 12-/24-hour clock preference
     *
     * @param context
     * @return
     */
    public static String getCurrentTimeString(Context context) {
        return DateFormat.getTimeFormat(context).format(new Date());
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return "";
        }
        return DATE_FORMAT_YYYYMMDD_HHMMSS_2.format(date);
    }

    public static String formatDate(Long date) {
        if (date == null || date == 0) {
            return "";
        }
        return DATE_FORMAT_YYYYMMDD_HHMMSS_2.format(new Date(date));
    }

    public static String formatDate(Date date, SimpleDateFormat simpleDateFormat) {
        if (date == null) {
            return "";
        }
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Long date, SimpleDateFormat simpleDateFormat) {
        if (date == null) {
            return "";
        }
        return simpleDateFormat.format(new Date(date));
    }

    public static String formatSecond(Date date, SimpleDateFormat simpleDateFormat) {
        if (date == null) {
            return "";
        }
        String time = new String(date.getTime() + "");
        if(time.length() <= SECOND_LENGTH){
            date.setTime(date.getTime() * 1000);
        }

        return simpleDateFormat.format(date);
    }

    public static String formatUTCTime(String UTCTime){
        long parse = parse(UTCTime, DATE_FORMAT_YYYYMMDD_T_HHMMSS_Z);
        if (parse == 0) {
            return UTCTime;
        } else {
            return formatDate(new Date(parse),DATE_FORMAT_YYYYMMDD_HHMMSS);
        }
    }

    public static long parse(String dateString, SimpleDateFormat simpleDateFormat) {
        return parse(dateString, simpleDateFormat, null);
    }

    public static long parse(String dateString, SimpleDateFormat simpleDateFormat, String timeZone) {
        if (!StringUtils.isNullOrEmpty(timeZone)) {
            simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
        }
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date == null ? 0 : date.getTime();
    }


    public static int compareDate(@Nullable Date o1, @Nullable Date o2) {
        if (o1 == null && o2 == null) {
            return 0;
        }
        if (o1 == null) {
            return -1;
        }
        if (o2 == null) {
            return 1;
        }
        return o1.compareTo(o2);
    }
}
